{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Divisible and Non-divisible Sums Difference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: differenceOfSums"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #分类求和并作差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个正整数 <code>n</code> 和 <code>m</code> 。</p>\n",
    "\n",
    "<p>现定义两个整数 <code>num1</code> 和 <code>num2</code> ，如下所示：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>num1</code>：范围 <code>[1, n]</code> 内所有 <strong>无法被 </strong><code>m</code><strong> 整除</strong> 的整数之和。</li>\n",
    "\t<li><code>num2</code>：范围 <code>[1, n]</code> 内所有 <strong>能够被 </strong><code>m</code><strong> 整除</strong> 的整数之和。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回整数 <code>num1 - num2</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 10, m = 3\n",
    "<strong>输出：</strong>19\n",
    "<strong>解释：</strong>在这个示例中：\n",
    "- 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ，num1 = 这些整数之和 = 37 。\n",
    "- 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ，num2 = 这些整数之和 = 18 。\n",
    "返回 37 - 18 = 19 作为答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 5, m = 6\n",
    "<strong>输出：</strong>15\n",
    "<strong>解释：</strong>在这个示例中：\n",
    "- 范围 [1, 5] 内无法被 6 整除的整数为 [1,2,3,4,5] ，num1 = 这些整数之和 =  15 。\n",
    "- 范围 [1, 5] 内能够被 6 整除的整数为 [] ，num2 = 这些整数之和 = 0 。\n",
    "返回 15 - 0 = 15 作为答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 5, m = 1\n",
    "<strong>输出：</strong>-15\n",
    "<strong>解释：</strong>在这个示例中：\n",
    "- 范围 [1, 5] 内无法被 1 整除的整数为 [] ，num1 = 这些整数之和 = 0 。 \n",
    "- 范围 [1, 5] 内能够被 1 整除的整数为 [1,2,3,4,5] ，num2 = 这些整数之和 = 15 。\n",
    "返回 0 - 15 = -15 作为答案。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n, m &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [divisible-and-non-divisible-sums-difference](https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [divisible-and-non-divisible-sums-difference](https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['10\\n3', '5\\n6', '5\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSums(self, n: int, m: int) -> int:\n",
    "        num1: int = 0\n",
    "        num2: int = 0\n",
    "\n",
    "        for i in range(1, n + 1):\n",
    "            if i % m != 0:\n",
    "                num1 += i\n",
    "            else:\n",
    "                num2 += i\n",
    "\n",
    "        return num1 - num2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSums(self, n: int, m: int) -> int:\n",
    "        num1 = 0\n",
    "        num2 = 0\n",
    "        i = 1\n",
    "        while i <= n:\n",
    "            if i % m == 0:\n",
    "                num2 += i\n",
    "            else:\n",
    "                num1 += i\n",
    "            i += 1\n",
    "        return (num1 - num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSums(self, n: int, m: int) -> int:\n",
    "        k=n//m\n",
    "        return n*(n+1)//2-k*(k+1)*m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSums(self, n: int, m: int) -> int:\n",
    "        res=0\n",
    "        for i in range(1,n+1):\n",
    "            if i%m==0:\n",
    "                res-=i\n",
    "            else:\n",
    "                res+=i\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
